package com.hexus.eve.actions.security;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.hexus.eve.entities.security.SecurityGroup;
import com.hexus.eve.entities.security.User;


import com.opensymphony.xwork2.ActionSupport;

public class EditUser extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private SessionFactory sessionFactory;
	private String userName;
	private User user = new User();
	
	@Override
	public String execute() throws Exception {
		if(userName != null) {
			HibernateTemplate ht = new HibernateTemplate(sessionFactory);
			user = ht.get(User.class, userName);
		}
		
		return SUCCESS;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public User getUser() {
		return user;
	}
	
	public List<SecurityGroup> getGroups(){
		HibernateTemplate ht = new HibernateTemplate(sessionFactory);
		return ht.loadAll(SecurityGroup.class);
	}
	
	public List<Integer> getSelectedGroups(){
		List<Integer> result = new ArrayList<Integer>();
		for(SecurityGroup g : user.getGroups()){
			result.add(g.getId());
		}
		return result;
	}	
}
